package com.bigdata;

import cn.hutool.core.collection.CollUtil;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.Catalog;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;

/**
 * DESC
 *
 * @author Cyber
 * <p> Created By 2025/5/12
 * @version 1.0
 */
@Slf4j
public class HmsClientOption {

    protected static final String HIVE_METASTORE_URIS = "thrift://127.0.0.1:9083";
//    protected static final String HIVE_METASTORE_URIS = "thrift://master:9083";

    protected static final String[] catalogNameList = new String[]{
            "xxx",
            "xxxx"
    };


    public static void main(String[] args) throws Exception {
        log.info(" HmsClientOption start ...");
        System.setProperty("HADOOP_USER_NAME", "root");
        Configuration conf = new Configuration();
        conf.set("hive.metastore.uris", HIVE_METASTORE_URIS);
        IMetaStoreClient client = HmsClient.init(conf);
        for (String catalogName : catalogNameList) {
            List<String> databases = client.getAllDatabases(catalogName);
            if (CollUtil.isNotEmpty(databases)) {
                for (String database : databases) {
                    List<String> tableList = client.getAllTables(catalogName, database);
                    if (CollUtil.isNotEmpty(tableList)) {
                        for (String table : tableList) {
                            client.dropTable(catalogName, database, table, true, true);
                            log.info("{} {} 删除表{} 成功!", catalogName, database, table);
                        }
                    }
                    client.dropDatabase(catalogName, database, true, true);
                    log.info("{} 删除数据库{}成功!", catalogName, database);
                }
                client.dropCatalog(catalogName);
                log.info("{} 删除Catalog成功!", catalogName);
            }
            try {
                Catalog isExist = client.getCatalog(catalogName);
                if (Objects.nonNull(isExist)) {
                    log.info("******************* {} 验证还已存在! *******************", catalogName);
                }
                log.info("================== {} 已验证不存在! ===================", catalogName);
            } catch (NoSuchObjectException e) {
                log.info("{} 已验证不存在!", catalogName);
            }
        }
    }
}